package com.nearby.android;

import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
//水平仪
public class Gradienter extends Activity implements SensorEventListener{
	MyView show;
	int MAX_ANGLE = 30;
	
	SensorManager mSensorManager;
	
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.gradienter);
		
		show = (MyView)findViewById(R.id.show);
		mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
	}
	
	@Override
	protected void onResume(){
		super.onResume();
		mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION), SensorManager.SENSOR_DELAY_GAME);
	}
	
	@Override
	protected void onPause(){
		mSensorManager.unregisterListener(this);
		super.onPause();
	}
	@Override
	protected void onStop(){
		mSensorManager.unregisterListener(this);
		super.onStop();
	}
	
	@Override
	public void onAccuracyChanged(Sensor sensor, int accuracy) {
	
		
	}

	@Override
	public void onSensorChanged(SensorEvent event) {
		float[] values = event.values;
		int sensorType = event.sensor.getType();
		switch(sensorType){
			case Sensor.TYPE_ORIENTATION:
				float yAngle = values[1];
				float zAngle = values[2];
				
				int x = (show.back.getWidth() - show.bubble.getWidth())/2;
				int y = (show.back.getHeight() - show.bubble.getHeight())/2;
				
				if(Math.abs(zAngle) <= MAX_ANGLE){
					int deltaX = (int) ((show.back.getWidth() - show.bubble
							.getWidth()) / 2 * zAngle / MAX_ANGLE);
						x += deltaX;
				}else if(zAngle > MAX_ANGLE){
					x = 0;
				}
				else
				{
					x = show.back.getWidth() - show.bubble.getWidth();
				}
			
				if (Math.abs(yAngle) <= MAX_ANGLE)
				{
					
					int deltaY = (int) ((show.back.getHeight() - show.bubble
						.getHeight()) / 2 * yAngle / MAX_ANGLE);
					y += deltaY;
				}
	
				else if (yAngle > MAX_ANGLE)
				{
					y = show.back.getHeight() - show.bubble.getHeight();
				}
				
				else
				{
					y = 0;
				}
				if (isContain(x, y))
				{
					show.bubbleX = x;
					show.bubbleY = y;
				}
			
				show.postInvalidate();
			break;
		}
		
	}
	// 计算x、y点的气泡是否处于水平仪的仪表盘内
	private boolean isContain(int x, int y)
	{
		// 计算气泡的圆心座标X、Y
		int bubbleCx = x + show.bubble.getWidth() / 2;
		int bubbleCy = y + show.bubble.getWidth() / 2;
		// 计算水平仪仪表盘的圆心座标X、Y
		int backCx = show.back.getWidth() / 2;
		int backCy = show.back.getWidth() / 2;
		// 计算气泡的圆心与水平仪仪表盘的圆心之间的距离。
		double distance = Math.sqrt((bubbleCx - backCx) * (bubbleCx - backCx)
			+ (bubbleCy - backCy) * (bubbleCy - backCy));
		// 若两个圆心的距离小于它们的半径差，即可认为处于该点的气泡依然位于仪表盘内
		if (distance < (show.back.getWidth() - show.bubble.getWidth()) / 2)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
}
